package edu.pku.hawk.controller.process;

import java.util.Timer;
import java.util.TimerTask;

import edu.pku.hawk.controller.detectmonitor.DetectReport;
import edu.pku.hawk.controller.util.Config;
import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.MQManager;
import edu.pku.hawk.controller.util.ProcDes;

public class ProcessController {

	MQManager mgr;
	ProcDes currProcessDes;

	public ProcessController() throws Exception {

		mgr = new MQManager(Constants.MQServer_Name, Constants.MQServer_Password, Constants.MQServer_URL);
//		mgr.createConsumerQueue(Constants.Figure_Monitor_Process_QueueName);
//		mgr.createConsumerQueue(Constants.Infrared_Monitor_Process_QueueName);
//		mgr.createConsumerQueue(Constants.Metal_Monitor_Process_QueueName);
//		mgr.createConsumerQueue(Constants.Pressure_Monitor_Process_QueueName);
//		mgr.createConsumerQueue(Constants.Smoke_Monitor_Process_QueueName);
//		mgr.createConsumerQueue(Constants.Sound_Monitor_Process_QueueName);
		
		currProcessDes = ProcDes.EmptyProcess();
	}

	public void run() {
		if (Config.UsingTimerOrSleep == true) {

			Timer timer = new Timer();
			TimerTask timerTask = new TimerTask() {
				public void run() {
					try {
						singleLoop();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			};
			timer.schedule(timerTask, 3000, Config.MonitorLoopInterval);

		} else {
			//
			while (true) {
				try {
					Thread.sleep(Config.MonitorLoopInterval);
					singleLoop();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	private void singleLoop() throws Exception {

		
		getCustomizedProcess() ;
		
//		Object o = mgr.getConsumerQueue(Constants.Smoke_Monitor_Process_QueueName).getObjectNoWait();
//		DetectReport report = (DetectReport) o;

		// .....
		

		if (processTriggerTest()) {

			//if(
			// trigger process

		}

		//

	}

	private boolean processTriggerTest() {
		return true;
	}
	
	
	private void getCustomizedProcess() {
		currProcessDes = ProcDes.defaultProcess();
	}

	public static void main(String[] args) {

	}
}
